package com.xiaoyu.izf.service;

import cn.hutool.core.util.StrUtil;
import com.xiaoyu.izf.model.MerchantPayChannel;
import com.xiaoyu.izf.model.MerchantZfbPayChannelInfo;
import com.xiaoyu.izf.repository.MerchantPayChannelDao;
import com.xiaoyu.izf.repository.MerchantZfbPayChannelDao;
import com.xiaoyu.izf.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;

@Service
public class MerchantZfbPayChannelInfoService {

    @Autowired
    private MerchantZfbPayChannelDao merchantZfbPayChannelDao;
    @Autowired
    private MerchantPayChannelDao merchantPayChannelDao;

    @Transactional
    public MerchantZfbPayChannelInfo save(MerchantPayChannel panChannel, HttpServletRequest request){
        MerchantZfbPayChannelInfo zi = merchantZfbPayChannelDao.findOneById(panChannel.getAssociatedId());
        zi.setInsertTime(new Timestamp(System.currentTimeMillis()));
        zi.setPicBankcard(CommonUtil.getWebPicUrl(request,zi.getPicBankcard()));
        zi.setPicBusinessLicence(CommonUtil.getWebPicUrl(request,zi.getPicBusinessLicence()));
        zi.setPicCashier(CommonUtil.getWebPicUrl(request,zi.getPicCashier()));
        zi.setPicDoorhead(CommonUtil.getWebPicUrl(request,zi.getPicDoorhead()));
        zi.setPicIdcardInhand(CommonUtil.getWebPicUrl(request,zi.getPicIdcardInhand()));
        zi.setPicLeaseContract(CommonUtil.getWebPicUrl(request,zi.getPicLeaseContract()));
        zi.setPicLegalpersonIdcardFront(CommonUtil.getWebPicUrl(request,zi.getPicLegalpersonIdcardFront()));
        zi.setPicLegalpersonIdcardReverse(CommonUtil.getWebPicUrl(request,zi.getPicLegalpersonIdcardReverse()));
        zi.setPicManageLocation(CommonUtil.getWebPicUrl(request,zi.getPicManageLocation()));
        zi.setPicOpenAccountLicence(CommonUtil.getWebPicUrl(request,zi.getPicOpenAccountLicence()));
        zi.setPicSettlemanIdcardFront(CommonUtil.getWebPicUrl(request,zi.getPicSettlemanIdcardFront()));
        zi.setPicSettlemanIdcardReverse(CommonUtil.getWebPicUrl(request,zi.getPicSettlemanIdcardReverse()));
        zi.setPicUnlegalpersonPrivateAuthletter(CommonUtil.getWebPicUrl(request,zi.getPicUnlegalpersonPrivateAuthletter()));
        if (StrUtil.isNotBlank(zi.getPicOtherinfo1())){
            zi.setPicOtherinfo1(CommonUtil.getWebPicUrl(request,zi.getPicOtherinfo1()));
        }
        if (StrUtil.isNotBlank(zi.getPicOtherinfo2())){
            zi.setPicOtherinfo2(CommonUtil.getWebPicUrl(request,zi.getPicOtherinfo2()));
        }

        zi = merchantZfbPayChannelDao.save(zi);
        panChannel.setOpened(2);
        panChannel.setSettleType(zi.getSettleType());
        merchantPayChannelDao.save(panChannel);
        return zi;
    }

    public MerchantZfbPayChannelInfo findOne(int id){
        return merchantZfbPayChannelDao.findOneById(id);
    }
}
